package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import easier.app.x.CommonCode;
import easier.window.picker.CalendarPicker;
import easier.window.picker.CalendarPicker.ActionListener;
import easier.window.picker.CalendarPickerFactory;
import java.util.Calendar;

public class MainActivity74 extends AppCompatActivity {

    private static final String TAG = "MainActivity74";

    private TextView mChoose;
    private TextView mConvert;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity74.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main74 );
        initView();
        initViewAction();
    }

    private void initView() {

        mChoose = (TextView) findViewById( R.id.choose );
        mConvert = (TextView) findViewById( R.id.convert );
    }

    private void initViewAction() {

        mChoose.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                CalendarPicker picker = CalendarPickerFactory.buildYearMonthDayPicker( v.getContext(), new ActionListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onDoneClick( Calendar calendar ) {

                        String ymd = CommonCode.getTimeStyleYMD( calendar );
                        mChoose.setText( ymd );

                        Log.d( TAG, "onDoneClick: " + calendar.getTime().getTime() );

                        try {
                            long l = CommonCode.convertFormatTimeToCalendar( ymd, "yyyy-MM-dd" );
                            mConvert.setText( String.valueOf( l ) );
                        } catch ( Exception e ) {
                            e.printStackTrace();
                            mConvert.setText( "" );
                        }
                    }
                } );
                picker.show( getSupportFragmentManager(), "选择日期" );
            }
        } );
    }
}